/*******************************************************************************
 *
 *		Azoteq SIMPLE_MOTION_Code
 *
 *              SIMPLE_MOTION_Code 
 *
*******************************************************************************/
#include "Project.h"
#include "simple_motion.h"
#include "debug.h"
#include "timer.h"

touch_t touch;

extern uint8 Gesture_status;
uint8 gesture_moving_flag;
uint8 last_press_gesture_code = 0;
extern uint32 numberOfFingers;
extern uint8 gesture_code;
extern CSD_Touchpad_POSITION_STRUCT fingerPosition[2];
static uint8 fingerTouchCnt = 0;
////////////////////////////////////////////////////////////////////////////////
/// \brief			isMoving
/// \param			
/// \return 			dir
/// 						 0:		no change
/// 						  1:		right
/// 						-1:		left
/// 						-2:		error							
/// \deprecated none
////////////////////////////////////////////////////////////////////////////////
int isMoving(uint16_t X,uint16_t Y)
{
	int dir= 0;
	int delta_x,delta_y,udelta_x,udelta_y;//,tmp
	uint16 last_x_pos = touch.last_x_pos;
	uint16 last_y_pos = touch.last_y_pos;
	
	delta_x = X - last_x_pos;
	delta_y = Y - last_y_pos;
	
	if(delta_x<0)
		{
			udelta_x = -delta_x;
		}
	else
		{
			udelta_x = delta_x;
		}
	
	if(delta_y<0)
		{
			udelta_y = -delta_y;
		}
	else
		{
			udelta_y = delta_y;
		}
	
	if((udelta_x >udelta_y)&& (udelta_x > MOVE_THR))
		{
      #if 0
			if(udelta_x > FAST_MOVE_THR)
				{
					delta_x =0;
					last_x_pos = X;
					last_y_pos = Y;
					dir = -1;
				}
        #endif
			if(delta_x>0)
			{
				//dir = GESTURE_LEFT;//left
        dir = GESTURE_UP;
        #if 0
        delta_x-=MOVE_THR; 
        #endif
			}
			else if(delta_x<0)
			{
				//dir = GESTURE_RIGHT;//right 	
        dir = GESTURE_DOWN;
        #if 0
        delta_x+=MOVE_THR; 
        #endif
			}
			#if 0
			touch.last_x_pos+=delta_x;

      if(touch.last_x_pos>10000)
      {
        touch.last_x_pos = 10000;
      }
      else if(touch.last_x_pos<10)
      {
       touch.last_x_pos = 10; 
        
      }
      
      touch.last_x_pos = X;
      #else
			touch.last_x_pos = X;
			touch.last_y_pos = Y;
			#endif
				
		}
	else if((udelta_x < udelta_y)&& (udelta_y > MOVE_THR))
		{
      #if 0
			if(udelta_y > FAST_MOVE_THR)
				{
					delta_y=0;
					//last_x_pos = X;
					//last_y_pos = Y;
					dir = -1;
				}
        #endif
			if(delta_y>0)
			{
        #if 0
        delta_y-=MOVE_THR; 
        #endif
        //dir = GESTURE_UP;//up 
        dir = GESTURE_LEFT;
			}
			else if(delta_y<0)
			{
				//dir = GESTURE_DOWN;//down
        #if 0
        delta_y+=MOVE_THR; 
        #endif
        dir = GESTURE_RIGHT;
			}
			#if 0
			touch.last_y_pos+=delta_y;

      if(touch.last_y_pos>10000)
      {
        touch.last_y_pos = 10000;
      }
      else if(touch.last_y_pos<10)
      {
       touch.last_y_pos = 10; 
        
      }
      
      touch.last_x_pos = X;
      #else
			touch.last_x_pos = X;
			touch.last_y_pos = Y;
			#endif

		}
		else
		{
			dir = 0;
		}
	
	return dir;


}


///////////////////////////////////////////////////////////////////////////////
 /// \brief 		 	 abs16
 /// \param 		 data
 /// \param 		
 /// \return		 data
 /// 
 ///////////////////////////////////////////////////////////////////////////////

uint16 abs16(int16 data)
{
  if(data<0)
    data = -data;

    return data ;
}

typedef struct{
	uint8 slider;
	uint8 now;
}test_buf_t;
//test_buf_t test_buf[100];
uint8 ptr=0;
////////////////////////////////////////////////////////////////////////////////
/// \brief			MotionGetFinger
/// \param			touch_t *touch
/// \return 			none
/// \deprecated none
////////////////////////////////////////////////////////////////////////////////
//		uint8 data_buffer[10];	
void MotionGetFinger(touch_t *touch)
{
    uint8 i;
    uint8 touch_cnt_r = 0;
    uint8 touch_pos_x = 0;
    uint8 touch_pos_y = 0;
    for(i=0;i<CSD_Touchpad_TOTAL_SENSOR_COUNT;i++)
    {
      if(CSD_Touchpad_sensorSignal[i]>15)	  
      {
        touch_cnt_r++;
      }
    }  
    if(touch_cnt_r >= 2)
    {
      touch->uch_P_Cnt = numberOfFingers;
      fingerTouchCnt++;
      Debug_Print(DEBUG_MESSAGE_LEVEL_3,"numberOfFingers = %d\r\n",numberOfFingers);
      Debug_Print(DEBUG_MESSAGE_LEVEL_4,"touch->uch_P_Cnt = %d,fingerPosition[0].x = %d,fingerPosition[0].y = %d\r\n",touch->uch_P_Cnt,fingerPosition[0].x,fingerPosition[0].y);
    }
    else
    {
      touch->uch_P_Cnt = 0;
    }
    
    
    
    
    
    #if 0

    for(i=0;i<CSD_Touchpad_TOTAL_SENSOR_COUNT-5;i++)
    {
      if(i == 4)
      {
        if(CSD_Touchpad_sensorSignal[4]>(CSD_Touchpad_sensorSignal[3]+5))
        {
          touch_pos_x = 4;
        }        
      }
      else
      {
        if(CSD_Touchpad_sensorSignal[i]>(CSD_Touchpad_sensorSignal[i+1]+5))
        {
          touch_pos_x = i;
        }        
      }
    }
    for(i=5;i<CSD_Touchpad_TOTAL_SENSOR_COUNT;i++)
    {
      if(i == 9)
      {
        if(CSD_Touchpad_sensorSignal[9]>(CSD_Touchpad_sensorSignal[8]+5))
        {
          touch_pos_y = 9;
        }           
      }
      else
      {
        if(CSD_Touchpad_sensorSignal[i]>(CSD_Touchpad_sensorSignal[i+1]+5))
        {
          touch_pos_y = i;
        }        
      }
    }   
    Debug_Print(DEBUG_MESSAGE_LEVEL_3,"touch->uch_P_Cnt = %d,touch->x_pos = %d,touch->y_pos = %d\r\n",touch->uch_P_Cnt,touch->x_pos,touch->y_pos);
    if(((touch_pos_x>=1)&&(touch_pos_x<=3))||((touch_pos_y>=6)&&(touch_pos_y<=8)))
    {
      touch->uch_P_Cnt = numberOfFingers;
    }
    else
    {
      touch->uch_P_Cnt =0;
    }    
    #endif    

    touch->x_pos = fingerPosition[0].x;
    touch->y_pos = fingerPosition[0].y;
    touch->gesture_status = gesture_code;

    //Debug_Print(DEBUG_MESSAGE_LEVEL_4,"touch->uch_P_Cnt = %d,touch->x_pos = %d,touch->y_pos = %d\r\n",touch->uch_P_Cnt,touch->x_pos,touch->y_pos);
}
////////////////////////////////////////////////////////////////////////////////
/// \brief			MotionSetFingerState
/// \param			touch_t *touch
/// \return 			none
/// \deprecated none
////////////////////////////////////////////////////////////////////////////////
void MotionSetFingerState(touch_t *touch)
{
	static uint8 last_touch_flag;
	uint8 now_touch_flag;
	if(touch->uch_P_Cnt)
	{
		now_touch_flag=TOUCH_IS_KEY;
	}
	else 
	{
		now_touch_flag = TOUCH_NO_KEY;
	}
	
	/* set finger message */
	if((last_touch_flag==TOUCH_NO_KEY) && (now_touch_flag!=TOUCH_NO_KEY))
	{
		touch->state_flag = STATE_DOWN;
	}
	else if((last_touch_flag !=TOUCH_NO_KEY ) &&(now_touch_flag!=TOUCH_NO_KEY))
	{
		touch->state_flag = STATE_PRESSED;		
	}
	else if((last_touch_flag !=TOUCH_NO_KEY ) && (now_touch_flag==TOUCH_NO_KEY))
	{
		touch->state_flag = STATE_UP; 			
	}
	else
	{
		touch->state_flag = STATE_NOFINGER;
	}

	last_touch_flag = now_touch_flag;
}


////////////////////////////////////////////////////////////////////////////////
/// \brief			MotionSetMotionMsg
/// \param			touch_t *touch
/// \return 			none
/// \deprecated none
////////////////////////////////////////////////////////////////////////////////
uint8 MotionSetMotionMsg(touch_t *touch)
{
	int16_t dlt_x,dlt_y,udelta_x,udelta_y;
	uint16_t Udlt_x,Udlt_y;
	int tmp_gesture=0;
	uint8 gesture = 0;
	
	switch(touch->state_flag)
		{
			case STATE_NOFINGER:
					touch->down_x_pos= 0;
					touch->down_y_pos= 0;		
					touch->last_x_pos= 0;
					touch->last_y_pos= 0;		
					gesture_moving_flag= 0;
          fingerTouchCnt = 0;
          //last_press_gesture_code = 0;
				break;
			case STATE_DOWN:	
				
				
			  Debug_Print(DEBUG_MESSAGE_LEVEL_4,"STATE_DOWN\r\n");
					touch->down_x_pos= touch->x_pos;
					touch->down_y_pos= touch->y_pos;		
					
					touch->last_x_pos= touch->x_pos;
					touch->last_y_pos= touch->y_pos;		
					
					touch->down_time = Timer_Get_Time_Stamp();
				break;
			case STATE_PRESSED:
			//	UartPrintf("STATE_PRESSED\n");	
					touch->last_x_pos= touch->x_pos;
					touch->last_y_pos= touch->y_pos;   
          Debug_Print(DEBUG_MESSAGE_LEVEL_3,"tap touch->gesture_status = %d\r\n",touch->gesture_status);
          #if 0
				if(Timer_Time_Elapsed(touch->down_time,250))
				{
//is moving
          tmp_gesture = isMoving(touch->x_pos,touch->y_pos);

          if(tmp_gesture != -1)
            {
            	gesture = tmp_gesture;
              gesture_moving_flag = 1;
            }
  				else
  					{
  						gesture_moving_flag = 0;
  					}
				}
        #endif
				break;
			case STATE_UP:
			Debug_Print(DEBUG_MESSAGE_LEVEL_4,"STATE_UP\r\n");
      Debug_Print(DEBUG_MESSAGE_LEVEL_3,"tap touch->gesture_status = %d\r\n",touch->gesture_status);
			//	if((touch->timer_count-touch->down_time )<350)//350ms
      #if 0
				if(!gesture_moving_flag)
					{
            if(touch->gesture_status == FLICK_UP_FLAG)
            {
              //gesture =  GESTURE_UP;
              gesture =  GESTURE_RIGHT;
              touch->repeat_en = 1;
            }
            else if(touch->gesture_status == FLICK_DOWN_FLAG)
            {
              //gesture =  GESTURE_DOWN;
              gesture =  GESTURE_LEFT;
              touch->repeat_en = 1;
            }   
            else if(touch->gesture_status == FLICK_LEFT_FLAG)
            {
              //gesture =  GESTURE_LEFT;
              gesture =  GESTURE_UP;
              touch->repeat_en = 1;
            }   
            else if(touch->gesture_status == FLICK_RIGHT_FLAG)
            {
              //gesture =  GESTURE_RIGHT;
              gesture =  GESTURE_DOWN;
              touch->repeat_en = 1;
            } 					
					}
    #endif
		#if 1
			//if(Timer_Time_Elapsed(touch->down_time,50))
      if(fingerTouchCnt>=3)
			{
        
        
					dlt_x = touch->last_x_pos - touch->down_x_pos;
					dlt_y = touch->last_y_pos - touch->down_y_pos;	
					
					Udlt_x = abs16(dlt_x);
					Udlt_y = abs16(dlt_y);

          Debug_Print(DEBUG_MESSAGE_LEVEL_3,"touch->down_x_pos = %d,touch->down_y_pos = %d\r\n",touch->down_x_pos,touch->down_y_pos);
          Debug_Print(DEBUG_MESSAGE_LEVEL_3,"touch->last_x_pos = %d,touch->last_y_pos = %d\r\n",touch->last_x_pos,touch->last_y_pos);
					if((Udlt_x>Udlt_y)&&(Udlt_x>MOVE_THR))
					{
						if(dlt_x>0)
						{
							touch->direction = GESTURE_UP;//GESTURE_UP
						  Debug_Print(DEBUG_MESSAGE_LEVEL_4,"gestrue up\r\n");

						}
						else
						{
							touch->direction = GESTURE_DOWN;//GESTURE_DOWN
							Debug_Print(DEBUG_MESSAGE_LEVEL_4,"gestrue down\r\n");

						}
					}
					else if((Udlt_y>=Udlt_x)&&(Udlt_y>MOVE_THR))
					{
						if(dlt_y>0)
						{
							touch->direction = GESTURE_RIGHT;//GESTURE_RIGHT
							Debug_Print(DEBUG_MESSAGE_LEVEL_4,"gestrue right\r\n");

						}
						else
						{
							touch->direction = GESTURE_LEFT;//GESTURE_LEFT
							Debug_Print(DEBUG_MESSAGE_LEVEL_4,"gestrue left\r\n");

						}
					}
					//else
						else if((Udlt_x<TAP_MOVE_THR)&&(Udlt_y<TAP_MOVE_THR))
					{
					    touch->direction = TAP;//tap
					    Debug_Print(DEBUG_MESSAGE_LEVEL_4,"gesture tap\r\n");

					}
          else
          {
 						touch->direction = 0x10;//0
						 Debug_Print(DEBUG_MESSAGE_LEVEL_4,"your gesture is not clear, please try again.\r\n");           
          }
          
          if(touch->gesture_status == 0x20)
          {
					    //touch->direction = TAP;//tap
					    //Debug_Print(DEBUG_MESSAGE_LEVEL_4,"tap gesture\r\n");            
          }
					touch->send_flag = 1;
				
#if 0
				if(!Timer_Time_Elapsed(touch->down_time,TAP_TIMEOUT))//tap time out
					{
					  if(touch->direction == TAP)
              touch->direction = TAP;
						Debug_Print(DEBUG_MESSAGE_LEVEL_4,"up tap\r\n");
					}
				else
					{
					if(touch->direction == TAP)
						touch->direction = 0;					
					}
#endif				
        gesture = touch->direction;
#if 0
				if(!Timer_Time_Elapsed(touch->down_time,Fast_SLIDER_TIMEOUT))//fast slider time out
				{
					if(touch->direction == GESTURE_LEFT)
						{Debug_Print(DEBUG_MESSAGE_LEVEL_4,"Fast_LEFT\r\n");	gesture = GESTURE_LEFT;	}
					else if(touch->direction == GESTURE_RIGHT)
						{Debug_Print(DEBUG_MESSAGE_LEVEL_4,"Fast_RIGHT\r\n");gesture = GESTURE_RIGHT;}
					else if(touch->direction == GESTURE_UP)
						{Debug_Print(DEBUG_MESSAGE_LEVEL_4,"Fast_UP\r\n");gesture = GESTURE_UP;}
					else if(touch->direction == GESTURE_DOWN)
						{Debug_Print(DEBUG_MESSAGE_LEVEL_4,"Fast_DOWN\r\n");gesture = GESTURE_DOWN;}
				}
				else if(!Timer_Time_Elapsed(touch->down_time,SLIDER_TIMEOUT))// slider time 
 				{
					if(touch->direction == GESTURE_LEFT)
						{Debug_Print(DEBUG_MESSAGE_LEVEL_4,"LEFT\r\n");gesture = GESTURE_LEFT;	}
					else if(touch->direction == GESTURE_RIGHT)
						{Debug_Print(DEBUG_MESSAGE_LEVEL_4,"RIGHT\r\n");gesture = GESTURE_RIGHT;}	
					else if(touch->direction == GESTURE_UP)
						{Debug_Print(DEBUG_MESSAGE_LEVEL_4,"UP\r\n");gesture = GESTURE_UP;	}
					else if(touch->direction == GESTURE_DOWN)
						{Debug_Print(DEBUG_MESSAGE_LEVEL_4,"DOWN\r\n");gesture = GESTURE_DOWN;	}
				}	
				else
					touch->direction = 0;		
#endif
			}
			#endif
			fingerTouchCnt = 0;		
				break;
					
			default:
				break;

		}

	return gesture;
}

////////////////////////////////////////////////////////////////////////////////
/// \brief			MotionSetMotionMsg
/// \param			touch_t *touch
/// \return 			none
/// \deprecated none
////////////////////////////////////////////////////////////////////////////////
void MotionInit(void)
{
	touch.timer_count = 0;
	touch.down_x_pos= 0;
	touch.down_y_pos= 0;		
}


////////////////////////////////////////////////////////////////////////////////
/// \brief			handleEvents
/// \param			none
/// \return 			none
/// \deprecated none
////////////////////////////////////////////////////////////////////////////////
uint8 handleEvents()
{
	uint8 gesture=0x10;
	MotionGetFinger(&touch);//get XY data
	MotionSetFingerState(&touch);//set state--down/press/up
	gesture = MotionSetMotionMsg(&touch);//deal Gesture 

	if(gesture<10)
		{
			Debug_Print(DEBUG_MESSAGE_LEVEL_3,"gesture =%d\r\n",gesture);
		}

	return gesture;
}

